﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using BLToolkit.Data;
using BLToolkit.Data.Linq;
using BLToolkit.DataAccess;
using BLToolkit.Mapping;
using Entities;
using Entities.IRepository;
using DynamicExpression = System.Linq.Dynamic.DynamicExpression;

namespace DAL
{
    public class SachBaoRepository:ISachBaoRepository
    {

        private readonly DbManager _dataContext;

        public int MaSB
        {
            get;
            set;
        }
		public SachBaoRepository()
		{
			_dataContext = new DbManager();
		}

        public Sach TimSachTheoMa(string maSB)
        {
            return _dataContext.GetTable<Sach>()
                .SingleOrDefault(t => t.MaSB == maSB);
        }

        public Bao TimBaoTheoMa(string maSB)
        {
            return _dataContext.GetTable<Bao>()
                .SingleOrDefault(t => t.MaSB == maSB);
        }

        public TapChi TimTapChiTheoMa(string maSB)
        {
            return _dataContext.GetTable<TapChi>()
                .SingleOrDefault(t => t.MaSB == maSB);
        }

		public List<SachBao> LayTatCa()
		{
            List<SachBao> l = new List<SachBao>();
            l.AddRange((_dataContext.GetTable<Sach>().ToList()));
            l.AddRange((_dataContext.GetTable<Bao>().ToList()));
            l.AddRange((_dataContext.GetTable<TapChi>().ToList()));
            return l;
		}

        public void Insertbatch(List<Sach> sach)
        {
            _dataContext.InsertBatch(sach);
        }
        public bool ThemSach(Sach sb)
        {
            return _dataContext.Insert(sb)>0;
        }
        public bool ThemBao(Bao sb)
        {
            return _dataContext.Insert(sb) > 0;
        }
        public bool ThemTapChi(TapChi sb)
        {
            return _dataContext.Insert(sb) > 0;
        }
        public bool SuaThongTinSach(Sach sb)
        {
            return _dataContext.Update(sb)>0;
        }
        public bool SuaThongTinBao(Bao sb)
        {
            return _dataContext.Update(sb) > 0;
        }
        public bool SuaThongTinTapChi(TapChi sb)
        {
            return _dataContext.Update(sb) > 0;
        }
        public bool XoaSach(Sach sb)
        {
            return _dataContext.Delete(sb) > 0;
        }

        public bool XoaSach(Expression<Func<Sach, bool>> predicate)
        {
            return _dataContext.GetTable<Sach>().Delete(predicate) > 0;
        }

        public bool XoaBao(Bao sb)
        {
            return _dataContext.Delete(sb) > 0;
        }

        public bool XoaBao(Expression<Func<Bao, bool>> predicate)
        {
            return _dataContext.GetTable<Bao>().Delete(predicate) > 0;
        }

        public bool XoaTapChi(TapChi sb)
        {
            return _dataContext.Delete(sb) > 0;
        }

        public bool XoaTapChi(Expression<Func<TapChi, bool>> predicate)
        {
            return _dataContext.GetTable<TapChi>().Delete(predicate) > 0;
        }

        public T GetData<T>(string query, bool isSP)
        {
            if (isSP)
                return _dataContext
                    .SetSpCommand(query)
                    .ExecuteObject<T>();
            else
                return _dataContext.SetCommand(query)
                    .ExecuteObject<T>();
        }

    }
}
